package it.controllocredito.frontend.activities;

import java.util.ArrayList;

import it.controllocredito.R;
import it.controllocredito.backend.persistence.CalllogPersistenceManager;
import it.controllocredito.backend.persistence.DatabaseHandler;
import it.controllocredito.backend.persistence.tables.RegistroChiamateTable;
import it.controllocredito.frontend.activities.MainFragmentActivity.TabsAdapter.TabInfo;
import it.controllocredito.frontend.fragments.InternetTabFragment;
import it.controllocredito.frontend.fragments.SmsTabFragment;
import it.controllocredito.frontend.fragments.SoglieTabFragment;
import it.controllocredito.frontend.fragments.CallTabFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

//public class MainFragmentActivity extends SherlockFragmentActivity {

	// private static Context mContext;
	// private static ActionBar mActionBar;
	// private static ViewPager mViewPager;
	// private static ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
	// private TabsAdapter mTabsAdapter;
	
	/** Called when the activity is first created. */
 // @Override
	// 	public void onCreate(Bundle savedInstanceState) {
		// super.onCreate(savedInstanceState);
		/*
		ActionBar bar = getSupportActionBar();
		bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		ActionBar.Tab tab1 = bar.newTab();
		ActionBar.Tab tab2 = bar.newTab();
		ActionBar.Tab tab3 = bar.newTab();
		ActionBar.Tab tab4 = bar.newTab();
		tab1.setText("Soglie");
		tab2.setText("Call");
		tab3.setText("Sms");
		tab4.setText("Dati");
		tab1.setTabListener(new MyTabListener());
		tab2.setTabListener(new MyTabListener());
		tab3.setTabListener(new MyTabListener());
		tab4.setTabListener(new MyTabListener());
		bar.addTab(tab1);
		bar.addTab(tab2);
		bar.addTab(tab3);
		bar.addTab(tab4);
		*/
		/*
		mActionBar = getSupportActionBar();
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		ActionBar.Tab tab1 = mActionBar.newTab();
		ActionBar.Tab tab2 = mActionBar.newTab();
		ActionBar.Tab tab3 = mActionBar.newTab();
		ActionBar.Tab tab4 = mActionBar.newTab();
		tab1.setText("Soglie");
		tab2.setText("Call");
		tab3.setText("Sms");
		tab4.setText("Dati");
		tab1.setTabListener(new MyTabListener());
		tab2.setTabListener(new MyTabListener());
		tab3.setTabListener(new MyTabListener());
		tab4.setTabListener(new MyTabListener());
		mActionBar.addTab(tab1);
		mActionBar.addTab(tab2);
		mActionBar.addTab(tab3);
		mActionBar.addTab(tab4);
		
	
		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mn_params:
			Intent openParamsActivity = new Intent(
					"it.controllocredito.frontend.activities.PARAMSACTIVITY");
			openParamsActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(openParamsActivity);
			overridePendingTransition(R.anim.slide_right, R.anim.slide_right);
			break;
		case R.id.mn_info:
			Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT)
					.show();
			break;
		case R.id.mn_dropTable:
			Toast.makeText(this, "Distruggo tutte le tabelle",
					Toast.LENGTH_LONG).show();

			DatabaseHandler databaseHandler = new DatabaseHandler(this);

			databaseHandler.dropTable(RegistroChiamateTable.getName());
			*/
			// databaseHandler.dropTable(MonitorTable.getName());
			/*
			break;
		case R.id.mn_create_update_table:
			Toast.makeText(this, "Creo e aggiorno tutte le tabelle",
					Toast.LENGTH_LONG).show();

			CalllogPersistenceManager calllogPersistenceManager = new CalllogPersistenceManager(
					this.getApplicationContext());

			calllogPersistenceManager.cleanRegistro();

			calllogPersistenceManager.aggiornamentoRegistro();
*/
			/*
			 * databaseHandler.addRecord(RegistroChiamateTable.getName(),
			 * RegistroChiamateTable.createRecord( CallLog.Calls.INCOMING_TYPE,
			 * 6000, "+39", "3477229171"));
			 */

			// databaseHandler.getRecordsRegistroChiamate();
			/*
			break;

		default:
			break;
		}

		return true;
	}

	private class MyTabListener implements ActionBar.TabListener {

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			switch (tab.getPosition()) {
			case 0:
				SoglieTabFragment soglie = new SoglieTabFragment();
				ft.replace(android.R.id.content, soglie);
				break;
			case 1:
				CallTabFragment call = new CallTabFragment();
				ft.replace(android.R.id.content, call);
				break;
			case 2:
				SmsTabFragment sms = new SmsTabFragment();
				ft.replace(android.R.id.content, sms);
				break;
			case 3:
				InternetTabFragment internet = new InternetTabFragment();
				ft.replace(android.R.id.content, internet);
				break;
			}
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
		}
	}

	public static class TabsAdapter extends FragmentPagerAdapter implements
			ActionBar.TabListener, ViewPager.OnPageChangeListener {

		static final class TabInfo {
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(Class<?> _class, Bundle _args) {
				clss = _class;
				args = _args;
			}
		}

		public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
			super(activity.getSupportFragmentManager());
			mContext = activity;
			mActionBar = activity.getSupportActionBar();
			mViewPager = pager;
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
			TabInfo info = new TabInfo(clss, args);
			tab.setTag(info);
			tab.setTabListener(this);
			mTabs.add(info);
			mActionBar.addTab(tab);
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);
			return Fragment.instantiate(mContext, info.clss.getName(),
					info.args);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			mActionBar.setSelectedNavigationItem(position);
		}

		@Override
		public void onPageScrollStateChanged(int state) {
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			Object tag = tab.getTag();
			for (int i = 0; i < mTabs.size(); i++) {
				if (mTabs.get(i) == tag) {
					mViewPager.setCurrentItem(i);
				}
			}
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
		}
	}

}

*/













public class MainFragmentActivity extends SherlockFragmentActivity {
    ViewPager mViewPager;
    TabsAdapter mTabsAdapter;
    TextView tabCenter;
    TextView tabText;
    ActionBar mActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);

        setContentView(mViewPager);
        mActionBar = getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mActionBar.setDisplayOptions(10, ActionBar.DISPLAY_SHOW_TITLE);
        mActionBar.setTitle("Controllo Credito");

        
        mTabsAdapter = new TabsAdapter(this, mViewPager);

        mTabsAdapter.addTab(mActionBar.newTab().setText("Soglie"), 
            SoglieTabFragment.class, null);
        mTabsAdapter.addTab(mActionBar.newTab().setText("Call"), 
        		CallTabFragment.class, null);
        mTabsAdapter.addTab(mActionBar.newTab().setText("Sms"), 
        		SmsTabFragment.class, null);
        mTabsAdapter.addTab(mActionBar.newTab().setText("Dati"), 
        		InternetTabFragment.class, null);
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getSupportMenuInflater();
      inflater.inflate(R.menu.main_menu, menu);
      return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
	      case R.id.mn_params:
	    	Intent openParamsActivity = new Intent("it.controllocredito.frontend.activities.PARAMSACTIVITY");
	    	openParamsActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        startActivity(openParamsActivity);
			overridePendingTransition(R.anim.slide_right,R.anim.slide_right);
	        break;
	      case R.id.mn_info:
	        Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT)
	            .show();
	        break;
	
	      default:
	        break;
      }

      return true;
    }
	
    public static class TabsAdapter extends FragmentPagerAdapter
            implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getSupportActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext,
                info.clss.getName(), info.args);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Object tag = tab.getTag();
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }
}